#include <vector>
#include <gflags/gflags.h>
#include "MsgRequester.h"
#include "proto_msg_handler.h"
#include "common.pb.h"
#include "CmdId.h"
#include "Head.h"
#include "SeqId.h"
#include "ResCode.h"
#include "device_request_distribute.pb.h"
#include "device.pb.h"
#include "proto_package.h"
#define  LOG_ERROR printf
#define CHECK_PTR_RETURN_NULL(ptr) if(NULL == ptr) {return;}
DEFINE_uint64(moduleid, E_MODULE_ID_DEVICE_MANAGER, "模块id");
CDevMngMsgHandler::CDevMngMsgHandler()
{
	m_className = "[CProtoMsgHandler]";
}


CDevMngMsgHandler::~CDevMngMsgHandler()
{	
	
}

CBackSvrMsgHandler::CBackSvrMsgHandler()
{
	m_className = "[CBackSvrMsgHandler]";
}


CBackSvrMsgHandler::~CBackSvrMsgHandler()
{

}

CMsgReqHandler::CMsgReqHandler()
{
	m_className = "[CWebDevReqMsgHandler]";
}


CMsgReqHandler::~CMsgReqHandler()
{

}

void CDevMngMsgHandler::HandleMsg(SessionPtr session, char* req, int32_t len)
{
	CHECK_PTR_RETURN_NULL(req);
	CHead parseHead;
	if (RERROR == parseHead.ParseHead(req, len))
	{
		LOG_ERROR("parseHead failed");
		return;
	}
	//判断缓存中是否够一个包
	if (len < CHead::GetHeadLen() + parseHead.m_nBodyLen)
	{
		LOG_ERROR("not enought package");
		return;
	}

	switch (parseHead.m_nCmd)
	{
	case E_CMD_CODE_DEVICEMANAGEREGISTER_RES:
	{
		QjDeviceQueryDistribute::DeviceManageRegisterRes  devMngRes;
		devMngRes.ParseFromArray(req + CHead::GetHeadLen(), len - CHead::GetHeadLen());
		LOG_INFO("[CDevMngMsgHandler][%0x] rev dev mnger reqister rsp code %d", this, devMngRes.rescode());

	}
	break;	
	case E_CMD_CODE_STOPVEDIOUPLOAD_REQ:	//视频上传终止
	{
		LOG_INFO("[CDevMngMsgHandler][%0x] rev stop video uplaod req", this);
		QjDevice::StartVedioUploadReq videoUploadReq;
		videoUploadReq.ParseFromArray(req + CHead::GetHeadLen(), len - CHead::GetHeadLen());
		SBuf msg;
		CProtoPackage::PackStopVedioUploadRsp(atoll(videoUploadReq.sessionid().c_str()), videoUploadReq.seqid(), 0, &msg);
		session->Write(msg.data(), msg.len());
	}
	break;
	case E_CMD_CODE_HEART_RES:
	{
		LOG_INFO("[CDevMngMsgHandler][%0x] rev heatbeat rsp\n", this);
	}
	break;
	default:
		break;
	}

}

void CBackSvrMsgHandler::HandleMsg(SessionPtr session, char* req, int32_t len)
{
	CHECK_PTR_RETURN_NULL(req);
	CHead parseHead;
	if (RERROR == parseHead.ParseHead(req, len))
	{
		LOG_ERROR("parseHead failed");
		return;
	}
	//判断缓存中是否够一个包
	if (len < CHead::GetHeadLen() + parseHead.m_nBodyLen)
	{
		LOG_ERROR("not enought package");
		return;
	}

	switch (parseHead.m_nCmd)
	{	
	case E_CMD_CODE_SERVERREGISTER_RES:
	{
		LOG_INFO("[CBackSvrMsgHandler][%0x] rev back svr reqister rsp", this);
	}
	break;
	case E_CMD_CODE_STOPVEDIOUPLOAD_REQ:	//视频上传终止
	{
		LOG_INFO("[CBackSvrMsgHandler][%0x] rev stop video uplaod req", this);
		QjDevice::StartVedioUploadReq videoUploadReq;
		videoUploadReq.ParseFromArray(req + CHead::GetHeadLen(), len - CHead::GetHeadLen());
		SBuf msg;
		CProtoPackage::PackStopVedioUploadRsp(atoll(videoUploadReq.sessionid().c_str()), videoUploadReq.seqid(), 0, &msg);
		session->Write(msg.data(), msg.len());
	}
	break;
	case E_CMD_CODE_HEART_RES:
	{
		LOG_INFO("[CBackSvrMsgHandler][%0x] rev heatbeat rsp", this);
	}
	break;
	default:
		break;
	}

}

void CMsgReqHandler::HandleMsg(SessionPtr session, char* req, int32_t len)
{
	CHECK_PTR_RETURN_NULL(req);
	CHead parseHead;
	if (RERROR == parseHead.ParseHead(req, len))
	{
		LOG_ERROR("parseHead failed");
		return;
	}
	//判断缓存中是否够一个包
	if (len < CHead::GetHeadLen() + parseHead.m_nBodyLen)
	{
		LOG_ERROR("not enought package");
		return;
	}

	switch (parseHead.m_nCmd)
	{
	break;
	case E_CMD_CODE_SERVERREGISTER_RES:
	{
		LOG_INFO("[CWebDevReqMsgHandler][%0x] rev back svr reqister rsp", this);	
	}
	break;
	case E_CMD_CODE_STOPVEDIOUPLOAD_RES:	//视频上传终止
	{
	#if 0
		QjDevice::StartVedioUploadRes uploadrsp;
		uploadrsp.ParseFromArray(req + CHead::GetHeadLen(), len - CHead::GetHeadLen());
		printf("[CWebDevReqMsgHandler][%0x] rev stop video uplaod rsp\n", this);
		usleep(100*1000);
		SBuf msg;		
		CProtoPackage::PackStopVedioUploadReq(m_destModuleID, m_moduleID, &msg);
		session->Write(msg.data(), msg.len());		
	#endif
	}
	break;
	case E_CMD_CODE_FILEUPLOADRESULT_RES:
	{
		LOG_INFO("file check result rsp");
	}		
	break;
	case E_CMD_CODE_FILECHECKING_RES:
	{
		LOG_INFO("file checking rsp");
	}
	break;	
	case E_CMD_CODE_HEART_RES:
	{
		LOG_INFO("[CWebDevReqMsgHandler][%0x] rev heatbeat rsp", this);
	}
	break;
	default:
		break;
	}

}